package controller

import (
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"mynetdisk/model"
	"mynetdisk/utils"
	"net/http"
	"os"
	"strconv"
	"strings"
)

/**
 *@Author tudou
 *@Date 2019/8/24
 **/

//var StaticPath="src/mynetdisk/statics/"

var StaticPath="./statics/"

func Login(c *gin.Context){
	session:=sessions.Default(c)
	username:=c.PostForm("username")
	password:=c.PostForm("password")
	if strings.Trim(username,"")==""||strings.Trim(password,"")==""{
		c.JSON(http.StatusOK,gin.H{"message":"please input the right information"})
		return
	}
	var user model.User
	if !user.GetUserByName(username)||user.Password!=password{
		c.JSON(http.StatusOK,gin.H{"message":"please input the right information"})
		return
	}
	session.Set("username",user.Username)
	session.Set("user",user.Id)
	err:=session.Save()
	if err!=nil {
		utils.CheckErr(err, "save the session failed")
		c.JSON(http.StatusOK,gin.H{"message":"find the unknown error"})
		return
	}
	c.JSON(http.StatusOK,gin.H{"message":"login successfully"})
}

func Register(c *gin.Context){
	username:=c.PostForm("username")
	password:=c.PostForm("password")
	nickname:=c.PostForm("nickname")
	var user model.User
	if user.GetUserByName(username){
		c.JSON(http.StatusOK,gin.H{"message":"the username have existed"})
		return
	}
	if user.GetUserNickName(nickname){
		c.JSON(http.StatusOK,gin.H{"message":"the nickname have existed"})
		return
	}
	if strings.Trim(username,"")==""||strings.Trim(password,"")==""{
		c.JSON(http.StatusOK,gin.H{"message":"please input the right information"})
		return
	}
	user=model.User{
		Username: username,
		Password: password,
		Nickname: nickname,
	}
	if !user.InsertUser(){
		c.JSON(http.StatusOK,gin.H{"message":"there is an unknown error"})
		return
	}
	var reUser model.User
	reUser.GetUserByName(username)
	mkdirPath:=StaticPath+strconv.Itoa(int(reUser.Id))
	//为该用户创建文件夹
	_=os.MkdirAll(mkdirPath,0777)
	c.JSON(http.StatusOK,gin.H{"message":"register successfully"})
}


func Logout(c *gin.Context){
	session:=sessions.Default(c)
	user:=session.Get("user")
	if user==nil{
		c.JSON(http.StatusBadRequest,gin.H{"error":"you must login before logout"})
		return
	} else{
	  session.Delete("user")
	  session.Delete("username")
	  session.Delete("nickname")
	  err:=session.Save()
	  if err!=nil{
	  	utils.CheckErr(err,"save the session failed")
	  	c.JSON(http.StatusBadRequest,gin.H{"error":"there is an unknown error"})
	  	return
	  }
	}
	c.JSON(http.StatusOK,gin.H{"message":"Successful logout"})
}


func CreateDir(c *gin.Context){
	session:=sessions.Default(c)
	userId:=session.Get("user").(uint)
	dirPath:=c.PostForm("dirPath")
	mkdirPath:=StaticPath+utils.ParseUintToString(userId)+dirPath
	err:=os.MkdirAll(mkdirPath,0777)
	if err!=nil{
		utils.CheckErr(err,"mkdir failed")
		c.JSON(http.StatusOK,gin.H{"message":"mkdir failed"})
		return
	}
	c.JSON(http.StatusOK,gin.H{"message":"mkdir successfully"})
}


func ShowDir(r *gin.RouterGroup)*gin.RouterGroup{
	var c *gin.Context
	session:=sessions.Default(c)
	userId:=session.Get("user").(uint)
	dirPath:=StaticPath+utils.ParseUintToString(userId)
	r.StaticFS("/file/own",http.Dir(dirPath))
	return r
}